package com.ruoyi.storage.service.impl;

import java.util.List;

import com.ruoyi.storage.domain.ItemDetail;
import com.ruoyi.storage.mapper.WarehouseInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.storage.domain.EntryDetail;
import com.ruoyi.storage.mapper.EntryRecordMapper;
import com.ruoyi.storage.domain.EntryRecord;
import com.ruoyi.storage.service.IEntryRecordService;

/**
 * 入库Service业务层处理
 * 
 * @author 庞一恺
 * @date 2025-01-20
 */
@Service
public class EntryRecordServiceImpl implements IEntryRecordService 
{
    @Autowired
    private EntryRecordMapper entryRecordMapper;
    @Autowired
    private WarehouseInventoryServiceImpl warehouseInventoryServiceImpl;
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;

    /**
     * 查询入库
     * 
     * @param entryRecordId 入库主键
     * @return 入库
     */
    @Override
    public EntryRecord selectEntryRecordByEntryRecordId(Integer entryRecordId)
    {
        return entryRecordMapper.selectEntryRecordByEntryRecordId(entryRecordId);
    }

    /**
     * 查询入库列表
     * 
     * @param entryRecord 入库
     * @return 入库
     */
    @Override
    public List<EntryRecord> selectEntryRecordList(EntryRecord entryRecord)
    {
        return entryRecordMapper.selectEntryRecordList(entryRecord);
    }

    /**
     * 新增入库
     * 
     * @param entryRecord 入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEntryRecord(EntryRecord entryRecord)
    {
        int rows = entryRecordMapper.insertEntryRecord(entryRecord);
        insertEntryDetail(entryRecord);
        return rows;
    }

    /**
     * 修改入库
     *
     * @param entryRecord 入库
     * @param zoneId
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEntryRecord(EntryRecord entryRecord, List<ItemDetail> itemDetailList, Integer zoneId)
    {
        entryRecordMapper.deleteEntryDetailByEntryRecordId(entryRecord.getEntryRecordId());
        insertEntryDetail(entryRecord);
        return entryRecordMapper.updateEntryRecord(entryRecord);
    }

    /**
     * 批量删除入库
     * 
     * @param entryRecordIds 需要删除的入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEntryRecordByEntryRecordIds(Integer[] entryRecordIds)
    {
        entryRecordMapper.deleteEntryDetailByEntryRecordIds(entryRecordIds);
        return entryRecordMapper.deleteEntryRecordByEntryRecordIds(entryRecordIds);
    }

    /**
     * 删除入库信息
     * 
     * @param entryRecordId 入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEntryRecordByEntryRecordId(Integer entryRecordId)
    {
        entryRecordMapper.deleteEntryDetailByEntryRecordId(entryRecordId);
        return entryRecordMapper.deleteEntryRecordByEntryRecordId(entryRecordId);
    }

    @Override
    public List<ItemDetail> getEntryItemDetailList(String batchId) {
        List<ItemDetail> itemDetailList = warehouseInventoryMapper.getItemDetailList(batchId);
        return itemDetailList;
    }

    /**
     * 新增入库详情信息
     * 
     * @param entryRecord 入库对象
     */
    public void insertEntryDetail(EntryRecord entryRecord)
    {
        List<EntryDetail> entryDetailList = entryRecord.getEntryDetailList();
        Integer entryRecordId = entryRecord.getEntryRecordId();
        if (StringUtils.isNotNull(entryDetailList))
        {
            List<EntryDetail> list = new ArrayList<EntryDetail>();
            for (EntryDetail entryDetail : entryDetailList)
            {
                entryDetail.setEntryRecordId(entryRecordId);
                list.add(entryDetail);
            }
            if (list.size() > 0)
            {
                entryRecordMapper.batchEntryDetail(list);
            }
        }
    }


}
